package com.example.module_home.paging

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.asLiveData
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.observe
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView
import com.example.module_home.R
import com.example.module_home.databinding.HomeActivityPagingBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class PagingActivity : AppCompatActivity() {

    private val pageViewModel: PageViewModel by viewModels()
    private lateinit var binding: HomeActivityPagingBinding
    private lateinit var pageAdapter: ArticlePageAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = HomeActivityPagingBinding.inflate(LayoutInflater.from(this), null, false)
        setContentView(binding.root)


        initPageAdapter()
    }

    private fun initPageAdapter() {

        pageAdapter = ArticlePageAdapter()

        binding.rcvPage.addItemDecoration(DividerItemDecoration(this,DividerItemDecoration.VERTICAL))
        binding.rcvPage.adapter = pageAdapter


        //lifecycleScope.launch {
            pageViewModel.articles.observe(this@PagingActivity, Observer{
                lifecycleScope.launchWhenCreated {
                    pageAdapter.submitData(it)
                }
            })
       // }
    }


}